package com.zmn.oms.task.job.orderupload;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.zmn.business.interfaces.order.OrderUploadIascBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 描述：每天2：30上传一次数据
 *
 * @author xiewenbing
 * @since 2022/8/4 13:49
 */
@LTS
@Slf4j
public class UploadIascJob {

    private static final String ULOAD_ORDER_IASC_CRON = "0 40 0 * * ?";

    @Resource
    TaskManager taskManager;

    @Autowired
    private OrderUploadIascBService uploadIascBService;

    /**
     * 提交任务
     */
    @PostConstruct
    void init() {

        Job job = new Job();
        job.setTaskId(TaskConsts.TASK_ID_ORDER_UPLOAD_IASC_JOB);
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_ORDER_UPLOAD_IASC_JOB);
        job.setCronExpression(ULOAD_ORDER_IASC_CRON);
        //存在则替换
        job.setReplaceOnExist(true);
        ResponseDTO<String> responseDTO = taskManager.submitTask2(job);
        if (!responseDTO.isSuccess()) {
            log.error("#" + TaskConsts.TASK_ID_ORDER_UPLOAD_IASC_JOB + "# job submit fail...");
        }
    }

    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_ORDER_UPLOAD_IASC_JOB)
    public Result runJob(Job job) {

        try {
            uploadIascBService.uploadOrderList();
            log.info("Upload order data to IASC end");
            return new Result(Action.EXECUTE_SUCCESS, "Upload success");
        } catch (Exception e) {
            log.error("Upload order data to IASC failed:[]", e.getMessage());
            return new Result(Action.EXECUTE_LATER, e.getMessage());
        }
    }
}
